﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

using YAMM.PlugIn;

namespace YAMM.PlugInManagement.Menu
{
    class ToolStripMenuCollection : IToolStripMenuCollection, System.Collections.IEnumerable
    {
        List<IToolStripMenu> menus;

        #region IToolStripMenuCollection Members

        public IToolStripMenu AddContextMenu(Guid identifier, string name)
        {
            throw new NotImplementedException();
        }
       
        public IToolStripMenu AddMenuBar(Guid identifier, string name)
        {
            YAMM.PlugInManagement.Menu.MenuBar.ToolStripMenu menu = new YAMM.PlugInManagement.Menu.MenuBar.ToolStripMenu();            
            menu.Identifier = identifier;
            menu.Name = name;
            if (identifier == PlugIn.StandardIdentifiers.MainMenu)
            {
                owner.Form.Controls.Add(menu);
                menu.GripStyle = ToolStripGripStyle.Hidden;
            }
            else
                owner.MenuContainer.TopToolStripPanel.Controls.Add(menu);
            menus.Add(menu);
            return menu;            
        }

        public IToolStripMenu AddToolBar(Guid identifier, string name)
        {
            YAMM.PlugInManagement.Menu.ToolBar.ToolStripMenu menu = new YAMM.PlugInManagement.Menu.ToolBar.ToolStripMenu();
            menu.Identifier = identifier;
            menu.Name = name;
            owner.MenuContainer.TopToolStripPanel.Controls.Add(menu);
            menus.Add(menu);
            return menu;
        }

        public bool Contains(Guid identifier)
        {
            bool retval = false;
            for (int i = 0; i < menus.Count; i++)
                if (identifier == menus[i].Identifier)
                {
                    retval = true;
                    break;
                }
            return retval;            
        }

        public void Remove(Guid identifier)
        {
            try
            {
                YAMM.PlugInManagement.Menu.MenuBar.ToolStripMenu m = this[identifier] as YAMM.PlugInManagement.Menu.MenuBar.ToolStripMenu;
                if (m != null)
                {
                    m.Parent.Controls.Remove(m);
                    m.Dispose();
                }
                else
                    throw new NotImplementedException();
                menus.Remove(m);
            }
            catch (ArgumentException)
            {
            }
            
        }

        public IToolStripMenu this[Guid identifier]
        {
            get 
            { 
                for (int i = 0; i < menus.Count; i++)
                    if (menus[i].Identifier == identifier)
                        return menus[i];
                throw new ArgumentException("Kein IToolStripMenu mit diesem identifier gefunden.");
            }
        }        

        public int Count
        {
            get { return menus.Count; }
        }

        #endregion        

        #region IEnumerable Members

        public System.Collections.IEnumerator GetEnumerator()
        {
            return menus.GetEnumerator();
        }

        #endregion

        public IToolStripMenu this[int index]
        {
            get { return menus[index]; }
        }

        ToolStripMenuManager owner;

        public ToolStripMenuCollection(ToolStripMenuManager owner)
        {
            this.owner = owner;
            this.menus = new List<IToolStripMenu>();
        }
    }
}
